import QtQuick
import QtQuick.Controls

Rectangle {
    id: editRect
    width: 200
    height: 400
    color: "#2e2f30"

    signal showProgress()

    property alias editTitle: title.text
    property int canPort: 0

    property var startIDList: [""]
    property var endIDList: [""]
    property var enFlagList: [0]

    property color mTextColor: "#e1e4e8"
    property int mTextSize: 14

    function setRecFilterID() {
        startIDList = user.getMyProperty("startIDList" + canPort.toString())
        endIDList = user.getMyProperty("endIDList" + canPort.toString())
        enFlagList = user.getMyProperty("enFlagList" + canPort.toString())
        resistanceChoose.currentIndex = user.getMyProperty("rFlag", canPort)
    }

    Component.onCompleted: {
        setRecFilterID()
    }

    Connections {
        target: user

        function onFilterIDChanged(index) {
            setRecFilterID()
        }
    }

    Row {
        id: filterIDEditRow
        width: parent.width
        height: 30
        padding: 6
        spacing: (width - title.width - readConfigBtn.width * 2 - resistanceChoose.width - padding * 2) / 4

        AsText {
            id: title
            height: parent.height
            text: qsTr("CAN1过滤ID配置:")
            font.pixelSize: mTextSize
            color: mTextColor
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
        }

        AsComboBox {
            id: resistanceChoose
            width: 80
            height: parent.height
            borderWidth: 0
            backDisplay: true
            textColor: mTextColor
            textSize: mTextSize
            model: ["120Ω", "无电阻"]

//            onCurrentIndexChanged: {
//                user.setMyProperty("rFlag", currentIndex, canPort)
//            }
        }

        AsButton {
            id: readConfigBtn
            width: 50
            height: parent.height
            text: qsTr("读取")
            font.pixelSize: mTextSize
            itemRadius: 8

            onClicked: {
                showProgress()
                user.sendSerialData("read", canPort, resistanceChoose.currentIndex === 0 ? true : false, 0)
            }
        }

        AsButton {
            id: writeConfigBtn
            width: 50
            height: parent.height
            text: qsTr("写入")
            font.pixelSize: mTextSize
            itemRadius: 8

            onClicked: {
                showProgress()
                user.sendSerialData("write", canPort, resistanceChoose.currentIndex === 0 ? true : false, 0)
            }
        }
    }

    ListView {
        id: editItem
        width: parent.width
        anchors.top: filterIDEditRow.bottom
        anchors.topMargin: filterIDEditRow.padding * 2
        anchors.bottom: parent.bottom
        clip: true
        spacing: 4

        model: 100
        delegate: Row {
            width: editItem.width
            height: 30
            spacing: (width - filterTitle.width - startIDText.width * 2 - text1.width - filterMode.width) / 5

            AsText {
                id: filterTitle
                width: 62
                height: parent.height
                text: "过滤组" + index + ":"
                color: mTextColor
                font.pixelSize: mTextSize
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
            }

            AsTextField {
                id: startIDText
                width: 70
                height: parent.height
                color: mTextColor
                text: startIDList[index]
                validator: RegularExpressionValidator { regularExpression: /[0-9A-F]{1,4}/ }
                placeholderText: qsTr("666")
                placeholderTextColor: "#666666"
                font.pixelSize: mTextSize

                onTextChanged: {
                    user.setMyProperty("startID" + canPort.toString(), text, index)
                    startIDList[index] = text
                }

                onFocusChanged: {
                    if(!focus) {
                        if(startIDList[index] !== "" && endIDList[index] !== "") {
                            var startID = user.hexToDec(text)
                            var endID = user.hexToDec(endIDList[index])
                            if(startID > endID) {
                                text = qsTr("")
                            }
//                            console.log(startID, endID, text)
                            user.setMyProperty("startID" + canPort.toString(), text, index)
                            startIDList[index] = text
                        }
                    }
                }
            }

            AsText {
                id: text1
                height: parent.height
                text: qsTr("---")
                color: mTextColor
                font.pixelSize: mTextSize
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
            }

            AsTextField {
                id: endIDText
                width: 70
                height: parent.height
                color: mTextColor
                text: endIDList[index]
                validator: RegularExpressionValidator { regularExpression: /[0-9A-F]{0,4}/ }
                placeholderText: qsTr("66F")
                placeholderTextColor: "#666666"
                font.pixelSize: mTextSize

                onTextChanged: {
                    user.setMyProperty("endID" + canPort.toString(), text, index)
                    endIDList[index] = text
                }

                onFocusChanged: {
                    if(!focus) {
                        if(startIDList[index] !== "" && endIDList[index] !== "") {
                            var startID = user.hexToDec(startIDList[index])
                            var endID = user.hexToDec(text)
                            if(startID > endID) {
                                text = qsTr("")
                            }
                            user.setMyProperty("endID" + canPort.toString(), text, index)
                            endIDList[index] = text
                        }
                    }
                }
            }

            AsComboBox {
                id: filterMode
                width: 70
                height: parent.height
                borderWidth: 0
                backDisplay: true
                textColor: mTextColor
                textSize: mTextSize
                model: ["失能", "使能"]
                currentIndex: enFlagList[index]

                onCurrentIndexChanged: {
                    user.setMyProperty("enFlag" + canPort.toString(), currentIndex, index)
                    enFlagList[index] = currentIndex
                }
            }

        }

        ScrollBar.vertical: AsScrollBar{}
    }
}
